package com.es.disped.web.controller.oj.contestproblem;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.oj.contestproblem.HustContestProblemService;
import com.es.disped.api.oj.problem.HustProblemService;
import com.es.disped.common.annotation.DynamicDataSource;
import com.es.disped.common.model.MsgModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.oj.model.ContestProblem;
import com.es.disped.dao.oj.model.ContestProblemExample;
import com.es.disped.dao.oj.model.Problem;
import com.es.disped.dao.oj.model.ProblemExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月17日 下午9:45:31<br>
 *  名称：ContestProblemController.java<br>
 *  描述：竞赛及作业题目管理<br>
 */
@Api("竞赛及作业题目管理")
@Controller
@RequestMapping("/manage/oj/contest/problem")
public class ContestProblemController extends BaseController{

	@Autowired
	HustContestProblemService hustContestProblemService;
	@Autowired
	HustProblemService hustProblemService;
	
	@ApiOperation(value="保存竞赛题目对象集合",response=MsgModel.class,httpMethod="POST",notes="批量添加竞赛或者作业题目")
	@RequiresPermissions(value={"teacher:save","admin:save"},logical=Logical.OR)
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@Transactional
	@DynamicDataSource
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="contestProblems",value="竞赛及作业题目对象集合",required=true) List<ContestProblem> contestProblems)
	{
		int count=0;
		for(ContestProblem record:contestProblems)
		{
			if(Optional.of(record)
					.filter(cId->record.getContestId()!=null)
					.filter(pId->record.getProblemId()!=null)
					.isPresent())
			{
				count+=hustContestProblemService.insertSelective(record);
			}
		}
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"竞赛作业题目添加成功":"竞赛作业题目添加失败",count>=1?contestProblems:"");
	}
	
	
	@ApiOperation(value="移除竞赛题目对象",response=MsgModel.class,httpMethod="POST",notes="移除竞赛或者作业题目，支持批量移除")
	@RequiresPermissions(value={"teacher:delete","admin:delete"},logical=Logical.OR)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="contestProblems",value="竞赛及作业题目对象集合",required=true) List<ContestProblem> contestProblems)
	{
		int count=0;
		List<Integer> pIds=new ArrayList<Integer>();
		for(ContestProblem record:contestProblems)
		{
			if(!this.isNull(record.getProblemId()))
			{
				pIds.add(record.getProblemId());
			}
		}
		ContestProblemExample deleteExample=new ContestProblemExample();
		ContestProblemExample.Criteria criteria=deleteExample.createCriteria();
		criteria.andProblemIdIn(pIds);
		count=hustContestProblemService.deleteByExample(deleteExample);
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"竞赛作业题目移除成功":"竞赛作业题目移除失败",count>=1?contestProblems:"");
	}
	
	@ApiOperation(value="根据竞赛/作业ID查询学生竞赛/作业列表",response=MsgModel.class,httpMethod="POST",notes="加载某一竞赛/作业的题目列表")
	@RequestMapping(value="/select",method=RequestMethod.POST)
	@DynamicDataSource
	public @ResponseBody MsgModel getContestProblem(@RequestBody @ApiParam(name="contestProblem",value="竞赛及作业学生对象",required=true)
													ContestProblem contestProblem)
	{
		List<ContestProblem> contestProblems=new ArrayList<ContestProblem>();
		if(!this.isNull(contestProblem.getContestId()))
		{
			ContestProblemExample contestProblemExample=new ContestProblemExample();
			ContestProblemExample.Criteria criteria=contestProblemExample.createCriteria();
			criteria.andContestIdEqualTo(contestProblem.getContestId());
			contestProblems=hustContestProblemService.selectByExample(contestProblemExample);
		}
		List<Integer> pIds=new ArrayList<Integer>();
		if(!this.isNull(contestProblems))
		{
			for(ContestProblem record:contestProblems)
			{
				if(!this.isNull(record.getProblemId()))
				{
					pIds.add(record.getProblemId());
				}
			}
		}
		ProblemExample problemExample=new ProblemExample();
		ProblemExample.Criteria criteria=problemExample.createCriteria();
		criteria.andProblemIdIn(pIds).andDefunctEqualTo("N");
		List<Problem> problems=hustProblemService.selectByExample(problemExample);
		return this.resultMsg(!this.isNull(problems)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(problems)?"学生作业加载成功":"学生作业加载失败",!this.isNull(problems)?problems:"");
	}
}
